package com.base.myproject.client.HR.recruitment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.BaseTabPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 操作人力申请表
 */
public class RecruitmentAudit extends BaseJDBCPanel {
	String tree_depcode = null;// 讲当前树形结构中选中的部门id保存
	TreePanel<TreeNodeModelData> treePanel = null;
	BaseTreeStore<TreeNodeModelData> treeWorkStore = null;
	DateField seach_begin_date = new DateField();// 查询开始日期
	DateField seach_end_date = new DateField();// 查询结束日期
	TextField<String> seach_name = new TextField<String>();// 姓名输入框

	Button seach_bt = new Button("查询");// 查询按钮
	Button bt1 = new Button("通过");// 批量通过按钮
	Button bt2 = new Button("不通过");// 批量不通过按钮
	Button request = new Button("申请职位"); // 申请选中职位的按钮

	BaseComboBoxX audit = new BaseComboBoxX("name", "dictionary",
			"where type='audit'"); // 审核状态

	BaseComboBoxForm<BaseModelData> sectionCode = new BaseComboBoxForm<BaseModelData>(
			"id", "name", "SystemInfo",
			"where len(id)=(select max(len(id)) from systeminfo)", true);
	// 部门名称

	List<Long> libraryList = new ArrayList<Long>(); // 存放通过简历库传过来的申请职位的所有简历icode

	String sql = "select * from hr_recruitmentRequest where isThrough is null and hirePeople is null order by requestDate asc";
	// 如果通过菜单打开该页面就使用这个SQL

	boolean through = false; // 判断是通过菜单打开该页面的还是通过简历库打开的，false代表菜单打开

	Dialog dialog = new Dialog(); // 弹出框，主要用于申请时让用户自己选择信息来源
	BaseComboBoxX box = new BaseComboBoxX("name", "dictionary",
			" where type = 'informationFrom'");// 信息来源的显示框

	public RecruitmentAudit() {
		init();
	}

	/**
	 * @param sql
	 *            通过简历库传过来的SQL，主要作用是只查通过审核的申请表，并设置页面默认显示的数据
	 *            这个构造方法主要用于不是从菜单打开这个页面时，用来设置页面默认显示的数据，并设置through为true，表示只查询通过
	 *            审核的，并为以为的查询做好准备条件
	 */
	public RecruitmentAudit(String sql) {
		this.sql = sql;
		through = true;
		audit.setEnabled(false);
		bt1.setEnabled(false);
		bt2.setEnabled(false);
		init();
	}

	public void init() {
		dialog.setHeading("请选择信息来源");
		dialog.add(box);
		dialog.setButtons(Dialog.OKCANCEL);
		dialog.getButtonById(Dialog.CANCEL)
		.setText("暂时不填写");
		dialog.setSize(200, 90);
		initTreePanel();
		initWeigetx();
		registerMainStore(store);
	}

	public void complete() {
		super.complete();
	}

	private void initWeigetx() {
		// 垂直布局
		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel s = new HorizontalPanel();

		s.setHeight("25");
		vp.setSpacing(5);

		s.add(new Label("查询时段:"));
		seach_begin_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		s.add(seach_begin_date);
		s.add(new Label("至"));
		seach_end_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		s.add(seach_end_date);
		s.add(new Label("匹配职位:"));
		s.add(seach_name);
		s.add(new Label("审核状态:"));
		s.add(audit);
		seach_begin_date.setWidth(127);
		seach_end_date.setWidth(127);
		seach_name.setWidth(127);
		audit.setWidth(60);
		seach_bt.addListener(Events.OnClick, getListener());
		s.add(seach_bt);
		bt1.addListener(Events.OnClick, getListener());
		s.add(bt1);
		bt2.addListener(Events.OnClick, getListener());
		s.add(bt2);
		s.add(request);
		request.addListener(Events.OnClick, getListener());

		vp.add(s);
		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.getBaseGrid());

		vp.add(panel2);
		HorizontalPanel cp2 = new HorizontalPanel();
		cp2.setSpacing(2);
		cp2.add(createForm());
		vp.add(cp2);

		ContentPanel center = new ContentPanel();
		center.setHeaderVisible(false);
		center.add(vp);

		ContentPanel panel = new ContentPanel();
		panel.setHeading("选择部门");
		panel.setCollapsible(true);
		panel.add(getTreePanel());

		HorizontalPanel cp = new HorizontalPanel();
		cp.setSpacing(5);
		cp.add(panel);
		cp.add(center);

		add(cp);
	}

	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;

	TreePanel<TreeNodeModelData> getTreePanel() {
		if (treePanel == null) {
			initTreePanel();
		}
		return treePanel;
	}

	BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}

	// 创建班次数据连接
	BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo si = new StoreInfo();
		si
				.setSql("select icode,id,name from SystemInfo where id is not null order by id asc");
		si.setTreesortcolumn("id");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("SystemInfo");
		treeStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		treeStore.load();
		return treeStore;

	}

	void initTreePanel() {
		treePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		Tools.setResize(treePanel, 0.18, 0.95);
		treePanel.setDisplayProperty("name");
		treePanel.setCheckStyle(CheckCascade.PARENTS);
		treePanel.getStyle().setLeafIcon(
				AbstractImagePrototype
						.create(BaseResource.INSTANCE.tree_file()));
		treePanel.setCheckNodes(CheckNodes.BOTH);
		treePanel.setCheckable(false);
		treePanel.setAutoLoad(true);
		treePanel.setCaching(true);

		treePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						String id = be.getItem().get("id").toString();
						tree_depcode = id;
						set_grid(id);
					}
				});
	}

	public void setCheckNodes(CheckNodes checkNodes) {
		treePanel.setCheckNodes(checkNodes);
	}

	void set_grid(String id) {
		StoreInfo info2 = new StoreInfo();
		info2.setTablename("hr_recruitmentRequest");
		info2.setKeyname(new String[] { "icode" });
		String where = " where 1=1 and ";
		if (id.length() < 5) {
			where = where + " depCode like '" + id + "%25'";
		} else {
			where = where + " depCode = '" + id + "'";
		}
		/*
		 * 通过through进行判断，如果为简历页面打开的，就只查询所有通过考核并还没成功招聘的记录， 如果不是简历页面打开的就
		 * 按审核状态打开相应的记录，默认为未审核的记录
		 */
		if (through) {
			info2
					.setSql("select * from hr_recruitmentRequest"
							+ where
							+ " and isThrough = 'true' and hirePeople is null order by requestDate asc"); // 这条sql用来得到已经通过审核的申请表
		} else {
			if (audit.getRawValue().equals("通过")) {
				info2
						.setSql("select * from hr_recruitmentRequest"
								+ where
								+ " and isThrough = 'true' and hirePeople is null order by requestDate asc"); // 这条sql用来得到已经通过审核的申请表
			} else if (audit.getRawValue().equals("不通过")) {
				info2
						.setSql("select * from hr_recruitmentRequest"
								+ where
								+ " and isThrough = 'false' and hirePeople is null order by requestDate asc"); // 这条sql用来得到没通过审核的申请表
			} else if (audit.getRawValue().equals("完成招聘")) {
				info2
						.setSql("select * from hr_recruitmentRequest"
								+ where
								+ " and hirePeople is not null order by requestDate asc"); // 这条sql用来得到完成招聘的申请表
			} else {
				info2
						.setSql("select * from hr_recruitmentRequest"
								+ where
								+ " and isThrough is null and hirePeople is null order by requestDate asc"); // 这条sql用来得到待审核的申请表
			}
		}
		grid.getStore().setStoreinfo(info2);
		grid.getStore().reload();
		grid.getView().refresh(true);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("requestNo");
		type.getField("requestNo").setType(String.class);
		type.addField("depCode");
		type.getField("depCode").setType(String.class);
		type.addField("requestDate");
		type.getField("requestDate").setType(Date.class);
		type.addField("position");
		type.getField("position").setType(String.class);
		type.addField("grade");
		type.getField("grade").setType(String.class);
		type.addField("explain");
		type.getField("explain").setType(String.class);
		type.addField("replaceEmpbyeeName");
		type.getField("replaceEmpbyeeName").setType(String.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		type.addField("age");
		type.getField("age").setType(Integer.class);
		type.addField("sex");
		type.getField("sex").setType(String.class);
		type.addField("education");
		type.getField("education").setType(String.class);
		type.addField("experience");
		type.getField("experience").setType(String.class);
		type.addField("skills");
		type.getField("skills").setType(String.class);
		type.addField("character");
		type.getField("character").setType(String.class);
		type.addField("hirePeople");
		type.getField("hirePeople").setType(String.class);
		type.addField("reportDate");
		type.getField("reportDate").setType(Date.class);
		type.addField("salary");
		type.getField("salary").setType(Double.class);
		type.addField("remark");
		type.getField("remark").setType(String.class);
		type.addField("isThrough");
		type.getField("isThrough").setType(Boolean.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_recruitmentRequest");
		info.setKeyname(new String[] { "icode" });
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore(), false);
		TableLayout tl = new TableLayout(2);

		TableData td = new TableData();
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("新增职位说明:"), td);
		TextArea C_num7 = new TextArea();
		C_num7.setWidth(670);
		C_num7.setHeight(187);
		C_num7.setName("explain");
		C_num7.setReadOnly(true);
		form.add(C_num7, td);
		form.inited();
		return form;
	}

	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}

		return grid;
	}

	CheckBoxSelectionModel<BusiObject> cbs;
	CheckColumnConfig C9;

	void createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("requestNo", "申请编号", 80);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("depCode", "申请部门", 80);
		C2.setRenderer(sectionCode);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("requestDate", "申请日期", 80);
		C3.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("position", "职位名称", 80);
		columns.add(C4);

		ColumnConfig C6 = new ColumnConfig("grade", "职位级别", 80);
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("education", "教育程度", 80);
		columns.add(C7);

		ColumnConfig C16 = new ColumnConfig("experience", "工作年限", 80);
		columns.add(C16);

		ColumnConfig C17 = new ColumnConfig("skills", "技能要求", 80);
		columns.add(C17);

		ColumnConfig C8 = new ColumnConfig("replaceEmpbyeeName", "取代员工", 80);
		columns.add(C8);

		ColumnConfig C11 = new ColumnConfig("status", "退职位原因", 80);
		columns.add(C11);

		ColumnConfig C14 = new ColumnConfig("age", "年龄", 40);
		columns.add(C14);

		ColumnConfig C15 = new ColumnConfig("sex", "性别", 40);
		columns.add(C15);

		ColumnConfig C18 = new ColumnConfig("character", "个性要求", 80);
		columns.add(C18);

		C9 = new CheckColumnConfig("isThrough", "审核状态", 60);
		columns.add(C9);

		ColumnConfig C20 = new ColumnConfig("hirePeople", "聘用人员", 80);
		TextField<String> text = new TextField<String>();
		C20.setEditor(new CellEditor(text));
		columns.add(C20);

		ColumnConfig C21 = new ColumnConfig("reportDate", "入职日期", 100);
		C21.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		DateField df = new DateField();
		df.getPropertyEditor()
				.setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C21.setEditor(new CellEditor(df));
		columns.add(C21);

		ColumnConfig C22 = new ColumnConfig("salary", "聘用薪金", 80);
		NumberField nf = new NumberField();
		nf.setPropertyEditorType(Double.class);
		C22.setEditor(new CellEditor(nf));
		columns.add(C22);

		ColumnConfig C23 = new ColumnConfig("remark", "聘用备注", 200);
		TextField<String> tx = new TextField<String>();
		C23.setEditor(new CellEditor(tx));
		columns.add(C23);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SIMPLE);
		grid.setSelectionModel(cbs);
		grid.addPlugin(cbs);
		grid.setBorders(false);
		grid.setSize(780, 230);
	}

	/************************************/

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(seach_bt)) {
								String bdate = seach_begin_date.getRawValue();
								String edate = seach_end_date.getRawValue();
								String name = seach_name.getRawValue();

								// 如果一个查询条件都没写的话，提示用户填写
								if ((bdate.equals("") || edate.equals(""))
										&& name.equals("")
										&& audit.getRawValue().equals("")) {
									MessageBox.alert("提示", "请填写其中一个完整查询条件!",
											null);
									return;
								}
								String where = "";

								// 判断是否得根据部门来查询
								if (tree_depcode != null) {
									where = " where  depCode like '"
											+ tree_depcode + "%25'";
								} else {
									where = " where 1=1";
								}

								// 用四个if来拼条件查询，得到不同的where语句
								if (!bdate.equals("")) {
									where += " and requestDate >= '" + bdate
											+ "'";
								}
								if (!edate.equals("")) {
									where += " and requestDate <= '" + edate
											+ "'";
								}
								if (!name.equals("")) {
									where += " and position like '%25" + name
											+ "%25'";
								}
								if (through) {
									where += " and isThrough = 'true' and hirePeople is null";
								} else {
									if (!audit.getRawValue().equals("")) {
										if (audit.getRawValue().equals("通过")) {
											where += " and isThrough = 'true' and hirePeople is null";
										} else if (audit.getRawValue().equals(
												"不通过")) {
											where += " and isThrough = 'false' and hirePeople is null";
										} else if (audit.getRawValue().equals(
												"完成招聘")) {
											where += " and hirePeople is not null";
										} else {
											where += " and isThrough is null and hirePeople is null";
										}
									}
								}

								String sql = "select * from hr_recruitmentRequest"
										+ where + " order by requestDate asc";
								grid.getStore().getStoreinfo().setSql(sql);
								grid.getStore().reload();
							}
							if (be.getSource().equals(bt1)) {
								auditOperation(true);
							}
							if (be.getSource().equals(bt2)) {
								auditOperation(false);
							}
							if (be.getSource().equals(request)) {
								if (grid.getSelectionModel().getSelectedItems()
										.size() != 1) {
									MessageBox.alert("提示",
											"每次只能申请一个职位，请重新选择职位!", null);
									cbs.deselectAll();
									return;
								}

								if (!store.getValue("isThrough").equals(true)) {
									MessageBox.alert("提示",
											"只能对审核通过的职位进行申请职位，请重新选择职位!", null);
									cbs.deselectAll();
									return;
								}
								/*
								 * 判断该页面是由简历页面打开的还是通过菜单打开的，如果是菜单打开的，
								 * 就再打开简历页面让用户选择简历进行申请职位。如果是简历页面打开的，
								 * 就直接生成sql语句，提交申请表
								 */
								if (libraryList.size() == 0) {
									String sql = "select * from hr_resumeLibrary where position = '"
											+ store.getValue("position")
											+ "' and not exists (select * from hr_positionRequest"
											+ " where resumeLibraryId = hr_resumeLibrary.icode and recruitmentId = '"
											+ store.getValue("icode") + "')"; // 这个SQL主要用于查询还没有申请选中职位的简历（已经申请过了的就不会显示）

									ResumeLibrary library = new ResumeLibrary(
											sql, (String) store
													.getValue("position"));
									library.setRecruitmentId((Long) store
											.getValue("icode"));
									library.complete();
									BaseTabPanel.getInstance().addTab("选择申请简历",
											library); // 在BasetabPanel上打开一个子tab
								} else {
									dialog.show();
									dialog
									.getButtonById(Dialog.CANCEL)
									.addSelectionListener(
											new SelectionListener<ButtonEvent>() {
												@Override
												public void componentSelected(
														ButtonEvent ce) {
													long id = (Long) store
															.getValue("icode");
													List<String> list = new ArrayList<String>();
													for (long libraryId : libraryList) {
														String sql = "insert into hr_positionRequest(recruitmentId,resumeLibraryId,requestDate) values("
																+ id
																+ ","
																+ libraryId
																+ ",getdate())"; // 已经有简历的，直接生成insert语句向申请表提交
														list.add(sql);
													}
													insertRequest(list);
													cbs.deselectAll();
													dialog.hide();
												}

											});
									dialog
											.getButtonById(Dialog.OK)
											.addSelectionListener(
													new SelectionListener<ButtonEvent>() {
														@Override
														public void componentSelected(
																ButtonEvent ce) {
															if (box
																	.getRawValue().equals("")) {
																MessageBox.alert("提示", "请选择信息来源，如果不先的请请点击‘暂时不填写’按钮，谢谢", null);
																return;
															}
																
															String informationFrom = box
																	.getRawValue();
															long id = (Long) store
																	.getValue("icode");
															List<String> list = new ArrayList<String>();
															for (long libraryId : libraryList) {
																String sql = "insert into hr_positionRequest(recruitmentId,resumeLibraryId,requestDate,informationForm) values("
																		+ id
																		+ ","
																		+ libraryId
																		+ ",getdate(),'"
																		+ informationFrom
																		+ "')"; // 已经有简历的，直接生成insert语句向申请表提交
																list.add(sql);
															}
															insertRequest(list);
															cbs.deselectAll();
															dialog.hide();
														}

													});

								}
							}
						}

					}

				} finally {
					sync--;
				}

			}

		};
		return defaultlistener;
	}

	/**
	 * @param isThrough
	 *            审核的结果（通过还是不通过）
	 *            审核结果如果为不通过的，一起设置审核状态的值。如果审核通过的，先通过发送RPC得到最大的申请表编号，
	 *            再循环设置审核状态的值和申请表编号
	 */
	public void auditOperation(final boolean isThrough) {
		if (grid.getSelectionModel().getSelectedItems().size() == 0) {
			MessageBox.alert("提示", "请选择一个以上的申请表进行操作，谢谢!", null);
		}
		if (isThrough) {
			String sql = "select isnull(max(requestNo),0) as max from hr_recruitmentRequest";
			final GreetingServiceAsync greetingService = GWT
					.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(DataSet result) {
					int incremental = 1;
					for (BusiObject md : grid.getSelectionModel()
							.getSelectedItems()) {
						String max = result.getValue(0, "max");
						int index = grid.getStore().indexOf(md);
						String numbers = getRequestNo(max, incremental);
						grid.setValue(index, "requestNo", numbers);
						grid.setValue(index, "isThrough", true);
						incremental++;
					}
					save();
					cbs.deselectAll();
				}
			});
		} else {
			for (BusiObject md : grid.getSelectionModel().getSelectedItems()) {
				int index = grid.getStore().indexOf(md);
				grid.setValue(index, "isThrough", true);
				grid.setValue(index, "isThrough", false);
				grid.setValue(index, "requestNo", null);
				save();
				cbs.deselectAll();
			}
		}
	}

	/**
	 * @param max
	 *            从RPC返回的最大申请表编号
	 * @param 最大申请表要增加的数量
	 * @return 审核通过的申请表编号 通过得到最大的编号，进行一系列的算法处理，得到6位数以上的编号
	 */
	private String getRequestNo(String max, int incremental) {
		int maxValue = Integer.parseInt(max);
		maxValue = maxValue + incremental;
		String maxString = String.valueOf(maxValue);
		String numbers = null;
		switch (maxString.length()) {
		case 1:
			numbers = "00000" + maxString;
			break;
		case 2:
			numbers = "0000" + maxString;
			break;
		case 3:
			numbers = "000" + maxString;
			break;
		case 4:
			numbers = "00" + maxString;
			break;
		case 5:
			numbers = "0" + maxString;
			break;
		default:
			numbers = maxString;
		}
		return numbers;
	}

	/**
	 * @param lists
	 *            要执行的所有insert语句
	 * @return 执行成功与否 执行所有的insert语句，完全申请表的提交操作 批量操作，一次让多个人申请选中职位
	 */
	public boolean insertRequest(List<String> lists) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		for (String sql : lists) {
			csql.getV().add(sql);
		}
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								Info.display("提示信息", "职位申请成功");
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}

	public List<Long> getLibraryList() {
		return libraryList;
	}

	public void setLibraryList(List<Long> libraryList) {
		this.libraryList = libraryList;
	}
}
